<?php

namespace app\model\v1\recruit;

use crmeb\traits\ModelTrait;
use think\Db;
use think\Exception;
use think\exception\ValidateException;
use think\Model;

class RecruitLanguage extends Model
{

    use ModelTrait;

    protected $hidden = [
   //     'create_time','update_time','delete_time'
    ];

    protected $updateTime = false;
    protected $createTime = false;


    const LANGUAGE_TYPE = 1;
    const USER_LANGUAGE_TYPE = 2;


    /**
     * 个人中心 - 获取常用语列表
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getRecruitLanguageList(int $uid)
    {
        $data["data"] = self::whereNull("delete_time")
            ->where("status",1)
            ->where("type", 1)
            ->order('sort','desc')
            ->select()->toArray();

        $data["custom"] = self::whereNull("delete_time")
            ->where("status",1)
            ->where("type", 2)
            ->where("user_id", $uid)
            ->order('sort','desc')
            ->select()->toArray();

        return $data;
    }


    /**
     * 个人中心 - 添加常用语
     * @param $param
     * @return int|string
     */
    public static function createLanguage($param = [])
    {
        $count = self::where("type", 2)->where("user_id", $param["user_id"])->count() ?? 0;
        if($count >= 5) Throw new Exception('自定义常用语最多5条');

        $sort = self::where("type", 1)->whereOr("user_id", $param["user_id"])->max("sort") ?? 0;

        $add_data = [
            "content" => $param["content"],  //常用语
            "type" => self::USER_LANGUAGE_TYPE, //用户自定义类型
            "sort" => $sort+1, //用户自定义添加的常用语
            "user_id" => $param["user_id"], //用户id
            "create_time" => time(), //创建时间
        ];
        return self::insert($add_data);
    }


    /**
     * 个人中心 - 修改常用语
     * @param $param
     * @return RecruitLanguage
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function updateLanguage($param = [])
    {
        $find = self::where("id", $param["id"])->find();
        if(empty($find)) Throw new Exception('没有此常用语！');  //当前id数据不存在
        if($find["user_id"] != $param["user_id"]) Throw new Exception('没有此常用语！');  //当前数据不存在

        $update["update_time"] = time(); //更新时间
        if(!empty($param["content"])) $update['content'] = $param["content"];
        if(!empty($param["sort"])) $update['sort'] = $param["sort"];
        return self::where('id', $param["id"])->update($update);
    }


    /**
     * 求职招聘后台 - 常用语管理列表
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getRecruitLanguageData($params = [])
    {
        $data["data"] = self::field("*, FROM_UNIXTIME(create_time) as create_time,
            case when status=1 then '启用' 
            when status=2 then '禁用' 
            end as status_text ")
            ->where(function ($query) use ($params){
                $query->whereNull('delete_time');
                $query->where("type", self::LANGUAGE_TYPE);
                if(!empty($params['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$params['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($params["word"])) $query->where("content",'like', '%'.$params["word"].'%');
            })
            ->order('id','desc')
            ->page($params['page'], $params['limit'])
            ->select()->toArray();

        $data["count"] = self::where(function ($query) use ($params){
                $query->whereNull('delete_time');
                $query->where("type", self::LANGUAGE_TYPE);
                if(!empty($params['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$params['time']);
                    $query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($params["word"])) $query->where("content",'like', '%'.$params["word"].'%');
            })
            ->count();

        return $data;
    }


    /**
     * 常用语管理列表 - 编辑
     * @param $param
     * @return RecruitLanguage
     */
    public static function updateRecruitLanguage($param = [])
    {
        $update["update_time"] = time();  //更新时间
        if(!empty($param["content"]))       $update["content"] = $param["content"];
        if(!empty($param["sort"]))          $update["sort"] = $param["sort"]; //类型

        return self::where("id", $param["id"])->update($update);
    }



    /**
     * 常用语管理列表 - 启用禁用
     * @param $param
     * @return string
     */
    public static function enableRecruitLanguage($param = [])
    {
        //获取用户发送的id，根据状态修改当前banner的启用禁用状态
        $status = self::where("id", $param['id'])->value("status");
        $msg = '';
        if($status == 1){   //1启用 2禁用
            $enable = 2;
            $msg = "禁用成功";
        }else{
            $enable = 1;
            $msg = "启用成功";
        }
        self::where("id", $param['id'])->update(['status' => $enable]);
        return $msg;
    }






    /**
     * 常用语管理列表 - 添加
     * @param $param
     * @return int|string
     */
    public static function addRecruitLanguage($param = [])
    {
        $add = [
            "content"       => $param["content"],
            "sort"          => $param["sort"] ?? '',
            "type"          => self::LANGUAGE_TYPE,
            "create_time"   => time(),
        ];
        return self::insert($add);
    }






}